/**
 * 基础菜单 商品管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>结算列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="类型">
        <el-select
          size="small"
          
          v-model="formInline.type"
          placeholder="请选择"
          @change="typeChange"
        >
          <el-option
            v-for="type in typeArr"
            :label="type.label"
            :value="type.value"
            :key="type.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名">
        <el-select
          size="small"
          clearable
          v-model="formInline.nameId"
          placeholder="请选择"
        >
          <el-option
            v-for="type in nameArr"
            :label="type.label"
            :value="type.value"
            :key="type.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
          size="small"
          v-model="formInline.date"
          type="month"
          value-format="yyyy-MM"
          placeholder="请选择日期"
          clearable
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <!-- <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEdit()"
          >添加</el-button
        > -->
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column sortable prop="type" label="类型" width="">
      </el-table-column>
      <el-table-column sortable prop="name" label="姓名" width="">
      </el-table-column>
      <el-table-column sortable prop="date" label="日期" width="">
      </el-table-column>
      <el-table-column sortable prop="settlementAmount" label="结算金额" width="">
      </el-table-column>
      <!-- <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="30%"
      @click="closeDialog"
      :close-on-click-modal="false"
    >
      <el-form
        label-width="120px"
        :model="editForm"
        :rules="rules"
        ref="editForm"
      >
        <el-form-item label="代理姓名" prop="agentName">
          <el-input
            size="small"
            v-model="editForm.agentName"
            auto-complete="off"
            placeholder="请输入代理姓名"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="部门代码" prop="deptNo">
          <el-input
            size="small"
            v-model="editForm.deptNo"
            auto-complete="off"
            placeholder="请输入部门代码"
          ></el-input>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('editForm')"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getSettlementList, listCustomeweOnline, getAgentList } from "../../api/userMG";
import Pagination from "../../components/Pagination";
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: "添加",
      editForm: {
        type:1, // 类型
        agentId:"", // 客户ID
      },
      // rules表单验证
      rules: {
        // agentName: [
        //   { required: true, message: "请输入客户姓名", trigger: "blur" },
        // ]
      },
      formInline: {
        pageNum: 1,
        pageSize: 10,
        type:"1", // 类型
        nameId:"", // 客户或者代理人下拉框Id
        date:"", // 年月
      },
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
      // 订单状态
      typeArr: [{
        value:"1",
        label:"客户"
      },{
        value:"2",
        label:"代理人"
      }],
      nameArr:[], // 姓名ID数组
      dataValue: "", // 时间
    };
  },
  // 注册组件
  components: {
    Pagination,
  },
  /**
   * 数据发生改变
   */
  mounted() {
    this.typeChange();
    this.getdata(this.formInline);
  },
  methods: {
    typeChange(){
      this.formInline.nameId = "";
      if(this.formInline.type == 1){ // 获取客户
        this.getlistCustomeweOnline();
      }else{ // 获取代理
        this.getAgentListFun();
      }
    },
    // 获取所有的客户
    getlistCustomeweOnline(){
      listCustomeweOnline().then(res=>{
        if (res.status != 200) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          this.nameArr = [];
          res.data.map(item=>{
            this.nameArr.push({
              label:item.customerName,
              value:item.customerId
            })
          })
          
        }
      })
    },
    // 获取所有的代理
    getAgentListFun(){
      getAgentList().then(res=>{
        if (res.status != 200) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          this.nameArr = [];
          res.data.map(item=>{
            this.nameArr.push({
              label:item.agentName,
              value:item.agentId
            })
          })
        }
      })
    },
    // 获取公司列表
    getdata(parameter) {
      getSettlementList(parameter)
        .then(res => {
          this.loading = false
          if (res.status != 200) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.listData = res.data.list;
            // 分页赋值
            this.pageparm.currentPage = Number(this.formInline.pageNum)
            this.pageparm.pageSize = Number(this.formInline.pageSize)
            this.pageparm.total = Number(res.data.total)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('数据加载失败，请稍后再试！')
        })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.pageNum = parm.currentPage;
      this.formInline.pageSize = parm.pageSize;
      this.getdata(this.formInline);
    },
    // 搜索事件
    search() { 
      this.formInline.pageNum = 1;
      this.getdata(this.formInline);
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        this.title = "修改";
        this.editForm.agentName = row.agentName; // 赋值里面的内容
        this.editForm.agentId = row.agentId; // 客户ID
      } else {
        this.title = "添加";
        this.editForm.agentId = "";
        this.editForm.agentName = ""; // 清空里面的内容
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate((valid) => {
        if (valid) {
          if(this.editForm.agentId == ""){
            let editData = {
              agentName:this.editForm.agentName
            }
            addAgent(editData)
            .then((res) => {
              this.editFormVisible = false;
              this.loading = false;
              if (res.status == "200") {
                this.getdata(this.formInline);
                this.$message({
                  type: "success",
                  message: res.desc,
                });
              } else {
                this.$message({
                  type: "info",
                  message: res.desc,
                });
              }
            })
            .catch((err) => {
              this.editFormVisible = false;
              this.loading = false;
              this.$message.error("接口调用失败，请稍后再试！");
            });
          }else{
            editAgent(this.editForm)
            .then((res) => {
              this.editFormVisible = false;
              this.loading = false;
              if (res.status == "200") {
                this.getdata(this.formInline);
                this.$message({
                  type: "success",
                  message: res.desc,
                });
              } else {
                this.$message({
                  type: "info",
                  message: res.desc,
                });
              }
            })
            .catch((err) => {
              this.editFormVisible = false;
              this.loading = false;
              this.$message.error("接口调用失败，请稍后再试！");
            });
          }
          
        } else {
          return false;
        }
      });
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 